import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'
import Layout from '../views/Layout.vue'
import Params from '../views/Params/Params.vue'
import Content from '../views/Content/Content.vue'
import Login from '../views/Login/Login.vue'
import store from '../store/index'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect:{name:'Home'},
    children:[
      {
        path: '/product',
        name: 'Home',
        component: Home,
        meta:{
          islogin:true
        }
        
      },
      {
        path: '/params',
        name: 'Params',
        component: Params,
        meta:{
          islogin:true
        }
      },
      {
        path: '/content',
        name: 'Content',
        component: Content,
        meta:{
          islogin:true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  }
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//前置守卫拦截

router.beforeEach((to,from,next)=>{
  if(to.meta.islogin){
    //在vuex中获取是否登陆
    var flag = store.state.token;
    if(flag){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})

export default router
